﻿using Microsoft.Extensions.Configuration;

namespace Nasty.Core.Config
{
    public static class SuperConfig
    {
        private static IConfiguration? _configuration { get; set; }

        //private static Dictionary<string, string> buckets = new Dictionary<string, string>();

        private static void TryInit()
        {
            if (_configuration != null) return;
            _configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).Build();
        }

        public static string GetBucketName(string key)
        {
            return Get($"FileS3:Bucket:{key}");
        }

        public static string Get(string key)
        {
            TryInit();
            return _configuration?.GetValue<string>(key) ?? string.Empty;
        }

        public static T? Get<T>(string key)
        {
            TryInit();
            var res = _configuration.GetSection(key).Get<T>();
            return res;
        }

        public static T Get<T>()
            where T : class, new()
        {
            TryInit();
            var instance = new T();
            _configuration?.Bind(instance);
            return instance;
        }
    }
}
